<?php
    
    include_once '../../controlador/php/conexion/Conexion10.php';
    $con = new Conexion10();
    
    $pacientes = "SELECT RTRIM(GENPACIEN.PACNUMDOC) AS ID,RTRIM(GENPACIEN.PACPRINOM) + ' ' + RTRIM(GENPACIEN.PACSEGNOM) + ' ' + 
                            RTRIM(GENPACIEN.PACPRIAPE) + ' ' + RTRIM(GENPACIEN.PACSEGAPE) AS 'PACIENTE', ADNINGRESO.AINTIPING,
                            CASE WHEN HPNDEFCAM.HCACODIGO  IS NULL THEN '' ELSE HPNDEFCAM.HCACODIGO END AS CAMA,HPNDEFCAM.OID AS OIDC
                 FROM ADNINGRESO INNER JOIN GENPACIEN ON GENPACIEN.OID = ADNINGRESO.GENPACIEN 
                                             LEFT OUTER JOIN HPNDEFCAM ON ADNINGRESO.HPNDEFCAM=HPNDEFCAM.OID 
                                                 LEFT OUTER JOIN HPNGRUPOS ON HPNDEFCAM.HPNGRUPOS=HPNGRUPOS.OID 
                                                 LEFT OUTER JOIN HPNSUBGRU ON HPNDEFCAM.HPNSUBGRU=HPNSUBGRU.OID 
                                                 LEFT OUTER JOIN HPNESTANC ON ADNINGRESO.OID=HPNESTANC.ADNINGRES AND HPNDEFCAM.OID=HPNESTANC.HPNDEFCAM  
                 WHERE ADNINGRESO.AINESTADO = 0
                       AND ((ADNINGRESO.AINURGCON = 0 AND ADNINGRESO.AINTIPING=1) OR 
                      (ADNINGRESO.AINURGCON = 0 AND ADNINGRESO.AINTIPING = 2 AND HPNDEFCAM.HCAESTADO NOT IN (3) 
                       AND HPNESTANC.HESFECSAL IS NULL)) AND HPNDEFCAM.OID IS NULL
                 ORDER BY 2";
    $res = $con->query($pacientes);
    
    while ($res2 = mssql_fetch_array($res)){
        
        $lista[] = array_map(utf8_encode,$res2);
    }
    
     print_r(json_encode($lista));
    
    $con->cerrarConexion();
    
?>